package com.kingdee.pda.ui.check_order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.basecommon.utils.PreferencesUtils;
import com.example.basecommon.utils.SpeakUtils;
import com.kingdee.pda.R;
import com.kingdee.pda.entity.CheckOrderListBean;
import com.kingdee.pda.entity.Warehouse;
import com.kingdee.pda.ui.activity.BaseTitleActivity;
import com.kingdee.pda.util.ToastUtils;

import java.util.List;

public class CheckOrderListActivity extends BaseTitleActivity implements View.OnKeyListener, CheckOrderListContract.ICheckOrderView{

    public static String CHECK_ORDER_TYPE_KEY = "check_order_type"; // 0 批量扫描 1 逐件扫描
    CheckOrderListPresenter presenter;
    ListView mListView;
    CheckOrderListAdapter mAdapter;
    private String[] titles = new String[]{"全部", "待初盘", "待复盘"};
    private TabLayout tabLayout;
    private EditText et_orderNo;
    private int selectPosition;
    public static String TAG = "CheckOrderList";
    RadioGroup rbGroup;
    RadioButton rbOne, rbAll;

    public static void actionStart(Context context) {
        context.startActivity(new Intent(context, CheckOrderListActivity.class));
    }
    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_check_order_list);
        presenter = new CheckOrderListPresenter(this,this);
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("库存盘点");
        mListView = findViewById(R.id.list_view);
        tabLayout = findViewById(R.id.tabLayout);
        et_orderNo = findViewById(R.id.et_orderNo);
        setEditTextScannerEvents(et_orderNo, this);
        rbGroup= findViewById(R.id.rb_group);
        rbOne = findViewById(R.id.rb_one);
        rbAll = findViewById(R.id.rb_all);
        if (PreferencesUtils.getInt(CheckOrderListActivity.this,CHECK_ORDER_TYPE_KEY) == 0){
            rbAll.setChecked(true);
        } else {
            rbOne.setChecked(true);
        }

        rbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_one){
//                    ToastUtils.showShort(CheckOrderListActivity.this, "one");
                    PreferencesUtils.putInt(CHECK_ORDER_TYPE_KEY,1);
                } else if (checkedId == R.id.rb_all){
                    PreferencesUtils.putInt(CHECK_ORDER_TYPE_KEY,0);
//                    ToastUtils.showShort(CheckOrderListActivity.this, "all");
                }
            }
        });
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                selectPosition = tab.getPosition();
                presenter.getCheckOrderList(getStatusByPosition(),"",getCurrentWarehouse().getId(),500,1);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


        mListView.setOnItemClickListener((parent, view, position, id) -> {
            Intent i = new Intent(CheckOrderListActivity.this, CheckOrderDetailActivity.class);
            startActivity(i);
        });
        findViewById(R.id.btn_query).setOnClickListener((v -> {
            String orderNo = et_orderNo.getText().toString().trim();
            if (!TextUtils.isEmpty(orderNo)){
                presenter.getCheckOrderList(getStatusByPosition(),orderNo,getCurrentWarehouse().getId(),500,1);
            } else {
                ToastUtils.showLong(this, "请输入要查询的盘点单号！");

            }
        }));
    }

    @Override
    protected void initData() {
        super.initData();
        mAdapter = new CheckOrderListAdapter(this);
        mListView.setAdapter(mAdapter);
        for (int i = 0; i < titles.length; i++) {
            tabLayout.addTab(tabLayout.newTab().setText(titles[i]));
        }

    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (!(v instanceof EditText)) {
            return false;
        }
        EditText editText = (EditText) v;
        String input = editText.getText().toString().trim();
        if ((keyCode==KeyEvent.KEYCODE_SYSRQ||keyCode == KeyEvent.KEYCODE_ENTER) && event.getAction() == KeyEvent.ACTION_UP) {
            if (v.getId() == R.id.et_orderNo) {
                presenter.getCheckOrderList(getStatusByPosition(), input, getCurrentWarehouse().getId(), 500, 1);
                return true;
            }
        }
        return false;
    }


    @Override
    protected void onResume() {
        super.onResume();
        tabLayout.getTabAt(0).select();
        et_orderNo.setText("");
    }

    @Override
    public void getOrderSuccess(List<CheckOrderListBean> list) {
        dismissLoadingDialog();
        Log.d(TAG,list.size()+"size");
        if (list != null && list.size() > 0) {
            mListView.setVisibility(View.VISIBLE);
            mAdapter.setData(list);
        } else {
            initNoDataView();
            SpeakUtils.speakWithToast(CheckOrderListActivity.this, "暂无数据", true, true);
        }
    }

    @Override
    public void getOrderFailed(String msg) {
        initNoDataView();
        ToastUtils.show(this,"请求失败，请重试"+msg, Toast.LENGTH_SHORT);
        SpeakUtils.speakWithToast(CheckOrderListActivity.this, msg, false, false);
    }

    @Override
    public void changeWarehouse(Warehouse warehouse) {
        presenter.getCheckOrderList(getStatusByPosition(), "", getCurrentWarehouse().getId(), 500, 1);

    }
    private String getStatusByPosition() {
        String status = "";
        if (selectPosition == 0) {
            status = "0";
        } else if (selectPosition == 1) {
            status = "3";
        } else if (selectPosition == 2) {
            status = "5";
        } else {
            status = "0";
        }
        return status;
    }

    private void initNoDataView(){
        mListView.setVisibility(View.GONE);
    }
}


